<?php

  class YaoqingmaAction extends CommonAction{
  
  
     public function index(){
	 
	   if(isset($_GET['status'])){
	   
	     $where['status']=array('eq',$_GET['status']);
	   }
	 
	   $where['uid']=array('eq',session('did'));
	 
	   $re=$this->page('yaoqingma','10','id desc',$where);
	   
	   $i=-1;
	   
	   foreach($re as $v){
	     $i++;
	     if($v['status']=="1"){
	   
	     $user=M('dailiuser')->where('yaoqingma="%s"',$v['yaoqingma'])->find();
		// dump($user);
		 $re[$i]['username']=$user['username'];
	   
	     }
	     
	   }
	   
	   
	   $this->re=$re;
	   
	   //计算邀请码的数量
	   
	   
	   $this->totalnum=M('yaoqingma')->where('uid="%s"',session('did'))->field('id')->count();
	   
	   
	 
	   $this->display();
	 
	 }
	 
	 
	 
	 public function buy(){
	 
	 
	   if(!empty($_POST)){
	   
	     $sys=M('system')->where('id=1')->find();
		 
	   
	     if($_POST['num']<1){
		   
		   $ajax=3;
		 
		   $this->ajaxReturn($ajax);
		 
		 }
		 
		 
	     $num=(int)$_POST['num'];
	   
	     $my=M('dailiuser')->where('id="%s"',session('did'))->find();
		 
		 //dump($num*$sys['yprice']);die;
		 
		 if($my['money']<($num*$sys['yprice'])){
		 
		     $ajax=5;      //('您的余额不足以购买这么多邀请码');
		     $this->ajaxReturn($ajax);
		 }
		 
		 $shibai=0;
		 
		// if(){
		 
		 
		 for($i=0;$i<$num;$i++){
		 
		   $str='1234567890abcdefghijklmnopqrstuvwxyz';
		   $str=str_shuffle($str);
		   $str=substr($str,0,8);
		   
		     $arr['yaoqingma']=$str;
		     $arr['uid']=session('did');
		     $arr['time']=time();
		   
		     $re=M('yaoqingma')->add($arr);
			 
			 if($re){
			 
			   $ajax=1;
			 
			 }else{
			   
			   $ajax=2;
			 
			 }
		   
		 }
		 
		 if($ajax=1){
		 
		   $smoney['money']=$my['money']-($num*$sys['yprice']);
		   
		   M('dailiuser')->where('id="%s"',session('did'))->save($smoney);
		   
		 }
		 
		
	     $this->ajaxReturn($ajax);
	   
	   }
	 
	   
	     $this->sys=M('system')->where('id=1')->find();
		 
		// dump($this->system);
	 
	   $this->display();
	 
	 }
	 
	 
	
  
  
  }
